package io.babystar.modules.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.babystar.common.utils.PageUtils;
import io.babystar.common.utils.Query;
import io.babystar.modules.business.dao.CWCategoryDao;
import io.babystar.modules.business.dao.CWOrderDao;
import io.babystar.modules.business.dao.ShopDao;
import io.babystar.modules.business.dao.StudentDao;
import io.babystar.modules.business.entity.CWCategoryEntity;
import io.babystar.modules.business.entity.CWOrderEntity;
import io.babystar.modules.business.entity.ShopEntity;
import io.babystar.modules.business.entity.StudentEntity;
import io.babystar.modules.business.service.CWOrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("cwOrderService")
public class CWOrderServiceImpl extends ServiceImpl<CWOrderDao, CWOrderEntity> implements CWOrderService {
    @Autowired
    private CWCategoryDao cwCategoryDao;
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private ShopDao shopDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String id = (String) params.get("id");
        String shopId = (String) params.get("shopId");
        Long businessId = (Long) params.get("businessId");
        String parentId = (String) params.get("parentId");
        String studentName = (String) params.get("studentName");
        String createStartDate = (String) params.get("createStartDate");
        String createEntDate = (String) params.get("createEntDate");
        IPage<CWOrderEntity> page = this.page(
                new Query<CWOrderEntity>().getPage(params),
                new QueryWrapper<CWOrderEntity>()
                        .eq("del_flag",0)
                        .eq(StringUtils.isNotBlank(id),"id",id)
                        .eq(businessId!=null,"business_id",businessId)
                        .eq(StringUtils.isNotBlank(shopId),"shop_id",shopId)
                        .eq(StringUtils.isNotBlank(parentId),"category_id",parentId)
                        .like(StringUtils.isNotBlank(studentName),"student_name",studentName)
                        .gt(StringUtils.isNotBlank(createStartDate),"create_at",createStartDate)
                        .le(StringUtils.isNotBlank(createEntDate),"create_at",createEntDate + " 23:59:59")
                        .orderByDesc("create_at")
        );
        List<CWOrderEntity> list = page.getRecords();
        list.stream().forEach(x -> {
            CWCategoryEntity categoryEntity = cwCategoryDao.selectById(x.getCategoryId());
            if(categoryEntity!=null){
                x.setCategoryName(categoryEntity.getCategory());
            }
            ShopEntity shopEntity = shopDao.selectById(x.getShopId());
            if(shopEntity!=null){
                x.setShopName(shopEntity.getShopName());
            }
        });
        return new PageUtils(page);
    }

}